package model

import (
	"jslog/example"
	"jslog/util"
)

type UserModel struct {
	Base
}

type User struct {
	Id            uint64 `gorm:"primaryKey;column:id;type:int(12)"`
	Username      string `gorm:"column:username;type:varchar(255);not null;"`
	Passwd        string `gorm:"column:passwd;type:varchar(64);not null"`
	Salt          string `gorm:"column:salt;type:varchar(64);not null"`
	Nickname      string `gorm:"column:nickname;type:varchar(255);not null"`
	Realname      string `gorm:"column:realname;type:varchar(255);not null"`
	Avatar        string `gorm:"column:avatar;type:varchar(255);not null"`
	Sex           uint8  `gorm:"column:sex;type:int(1);not null"`
	Age           uint8  `gorm:"column:age;type:int(3);not null"`
	Phone         string `gorm:"column:phone;type:varchar(12);not null"`
	Email         string `gorm:"column:email;type:varchar(255);not null"`
	Lastloginip   string `gorm:"column:lastloginip;type:varchar(20);not null"`
	Lastlogintime int64  `gorm:"column:lastlogintime;type:varchar(255);not null"`
	Status        uint8  `gorm:"column:status;type:int(1);not null"`
	Identify_comm string `gorm:"column:identify_comm;type:varchar(255);not null"`
}

func (m *UserModel) TabName() string {
	return example.JSLOG_CONF.Mysql.Prefix + "user"
}

// 查询用户详情
func (m *UserModel) Detail(id uint64, where *User) (User, error, bool) {

	var user User
	userWhere := &User{}
	if id > 0 {
		userWhere.Id = id
	} else {
		userWhere = where
	}
	err := example.JSLOG_DB.Where(&userWhere).Order("id DESC").Find(&user)
	if err.Error != nil {
		return user, err.Error, false
	} else {
		if user.Id > 0 {
			return user, nil, true
		}
	}
	return user, nil, false
}

// 查询用户列表
func (m *UserModel) ListData(id uint64, where map[string]string) ([]User, error, bool) {

	var user []User
	err := example.JSLOG_DB.Where(&User{Phone: where["phone"]}).Order("id DESC").Find(&user)
	if err.Error != nil {
		return user, err.Error, false
	}
	return user, err.Error, true
}

// 根据条WHERE IN查询
func (m *UserModel) UserList(wIn []uint64, last string) ([]User, error) {

	var user []User
	err := example.JSLOG_DB.Find(&user, wIn)
	if err.Error != nil {
		return nil, err.Error
	}
	return user, nil
}

// 新增用户
func (m *UserModel) Auserdd(data map[string]string) bool {

	data["salt"] = util.Salt(6)
	var randstr string = util.RandStr()
	user := User{
		Passwd:        util.Passwd(0, data),
		Salt:          data["salt"],
		Lastloginip:   data["lastloginip"],
		Lastlogintime: util.Time(),
		Identify_comm: util.Md5(randstr + util.Int64ToStr(int64(util.Nanosecond())) + randstr + util.Int64ToStr(int64(util.Nanosecond()))),
		Status:        0,
	}
	if data["account_type"] == "mail" {
		user.Email = data["account"]
	}
	if data["account_type"] == "phone" {
		user.Phone = data["account"]
	}
	result := example.JSLOG_DB.Create(&user)
	return result.Error == nil
}

// 编辑
func (m *UserModel) Edit(id string, data map[string]string) bool {

	if id == "" || len(data) < 1 {
		return false
	}
	where := &User{Id: util.StrToUint64(id)}
	var userData User
	if data["username"] != "" {
		userData.Username = data["username"]
	}
	if data["username"] != "" {
		userData.Username = data["username"]
	}
	upDate := example.JSLOG_DB.Model(where).Updates(userData)
	return upDate.Error == nil
}
